import Echo from "laravel-echo";
import Api from '@/api/auth'
import Pusher from "pusher-js";
window.Pusher = Pusher;
export const setupEcho = () => {
    window.Echo = new Echo({
        broadcaster: 'pusher',
        key: import.meta.env.VITE_PUSHER_APP_KEY,
        cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER, // required if the broadcaster is pusher.
        wsHost: import.meta.env.VITE_PUSHER_HOST,
        wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
        wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
        forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
        enabledTransports: ['ws', 'wss'],
        authorizer: (channel, options) => {
            return {
                authorize: async (socketId, callback) => {
                    try {
                        const data = await Api.echoAuth({ socket_id: socketId, channel_name: channel.name })
                        callback(null, data);
                    } catch (error) {
                        callback({});
                    }
                }
            };
        },
    });
}

